package com.vogue.meeting.common.web.exception.handler;

import com.vogue.meeting.common.web.domain.Result;
import com.vogue.meeting.common.web.domain.ResultCode;
import com.vogue.meeting.common.web.exception.business.token.TokenExpiredException;
import org.springframework.core.annotation.Order;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.common.exceptions.InvalidRequestException;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.security.oauth2.common.exceptions.UnsupportedGrantTypeException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Token相关
 */
@Order(1)
@RestControllerAdvice
public class TokenExceptionHandler {

    @ExceptionHandler(BadCredentialsException.class)
    public Result badCredentialsException(BadCredentialsException e){
        return Result.failure(ResultCode.USER_BAD_CREDENTIALS);
    }

    @ExceptionHandler(UsernameNotFoundException.class)
    public Result usernameNotFoundException(UsernameNotFoundException e){
        return Result.failure(ResultCode.USER_USERNAME_NOT_FOUND.code(),e.getMessage());
    }

    @ExceptionHandler(AccessDeniedException.class)
    public Result accessDeniedException(AccessDeniedException e){
        return Result.failure(ResultCode.NOT_PERMISSION);
    }

    @ExceptionHandler(InvalidRequestException.class)
    public Result invalidRequestTokenException(InvalidRequestException e){
        return Result.failure(ResultCode.REQUEST_INVALID.code(),e.getMessage());
    }
    @ExceptionHandler(InvalidTokenException.class)
    public Result invalidTokenException(InvalidTokenException e){
        return Result.failure(ResultCode.TOKEN_INVALID.code(),e.getMessage());
    }

    @ExceptionHandler(CredentialsExpiredException.class)
    public Result credentialsExpiredException(CredentialsExpiredException e){
        return Result.failure(ResultCode.TOKEN_EXPIRED);
    }

    @ExceptionHandler(InsufficientAuthenticationException.class)
    public Result insufficientAuthenticationException(InsufficientAuthenticationException e){
        return Result.failure(ResultCode.INSUFFICIENT_AUTHENTICATION);
    }

/*    @ExceptionHandler(UnsupportedGrantTypeException.class)
    public Result unsupportedGrantTypeException(UnsupportedGrantTypeException e){
        return Result.failure(ResultCode.UNSUPPORTED_GRANT_TYPE);
    }*/

}
